import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { RouterModule, Routes, Router, ActivatedRoute, ParamMap } from '@angular/router';
import { MdlDialogService, MdlDialogReference, MdlSnackbarService } from '@angular-mdl/core';
import { ConfirmationService, SelectItem } from 'primeng/primeng';
import { RoleService } from './service/role.service';
import { flyIn } from '../../common/animation/common.animation';
import { switchMap } from 'rxjs/operators';
@Component({
    templateUrl: './role-list.component.html',
    styleUrls: ['./role-list.component.scss'],
    providers: [RoleService],
    animations: [flyIn]
})
export class RoleListComponent implements OnInit {
    unitId: number;
    display: boolean = false;
    allItems: any[];
    showItems: any[];
    selectedItem: any;
    searchItem: any = {};
    searchForm: FormGroup;

    constructor(
        private roleService: RoleService,
        private router: Router,
        private route: ActivatedRoute,
        private fb: FormBuilder,
        private snackbarService: MdlSnackbarService,
        private confirmationService: ConfirmationService) { }

    ngOnInit() {
        this.route.paramMap.pipe(
            switchMap((params: ParamMap) => {
                this.unitId = +params.get('id');
                return this.roleService.getRolesByUnitId(+this.unitId);
            })
        ).subscribe(data => {
            data.forEach((role, i) => role['seq'] = (i + 1));
            this.allItems = this.showItems = data;
            this.roleSelect(data[0]);
        }, error => console.error(error));
        this.buildForm();
    }

    /** 岗位选择事件 */
    roleSelect(role: any) {
        this.showItems.forEach(r => {
            if (role.roleId == r.roleId) {
                r.checkStyle = { 'background-color': 'rgb(3,169,244)' }
                this.selectedItem = r;
            } else {
                r.checkStyle = { 'background-color': 'whitesmoke' }
            }
        });
    }

    /** 修改 */
    update($event) {
        if (this.selectedItem) {
            this.router.navigate(['/system/role/info', { "id": this.selectedItem.roleId }]);
        }
    }

    /** 新增 */
    add($event) {
        this.router.navigate(['/system/role/info', { 'unitId': this.unitId }]);
    }

    /** 删除 */
    delete($event) {
        if (this.selectedItem) {
            this.confirmationService.confirm({
                header: '确认',
                message: '您确定要删除选择的记录吗？',
                accept: () => {
                    this.roleService.delete(this.selectedItem.roleId).subscribe(data => {
                        this.snackbarService.showToast("数据删除成功！");
                        this.showItems.forEach((r, index) => {
                            if (this.selectedItem.roleId == r.roleId)
                                this.showItems.splice(index, 1);
                        });
                        this.selectedItem = null;
                    })
                }
            });
        }
    }

    /**查询 */
    search($event) {
        this.searchItem = this.searchForm.value;
        this.showItems = [];
        this.allItems.forEach(item => {
            if (item.roleName.indexOf(this.searchItem.roleName) != -1
                && item.roleId + "".indexOf(this.searchItem.roleId + "") != -1) {
                this.showItems.push(item);
            }
        });
    }
    buildForm(): void {
        this.searchForm = this.fb.group({
            'roleId': [this.searchItem.roleId],
            'roleName': [this.searchItem.roleName]
        });
    }
}
